package com.epoint.netty.future;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class NettyFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 可以看成线程池（这里也可以换成NioEventLoop）
        DefaultEventLoop eventExecutors = new DefaultEventLoop();

        Future<Integer> submit = eventExecutors.submit(() -> {
            TimeUnit.SECONDS.sleep(3);
            return 55;
        });

        // 同步获取数据（和jdk得方式基本一样）
//        System.out.println("主线程~~~");
//        System.out.println(submit.get());

        // 异步获取数据：注册回调事件的方法来消费 上面的返回值
        submit.addListener((e)->{
            System.out.println("future获取得数据：" + e.get());
            System.out.println("子线程~~");
        });

        System.out.println("主线程~~");
        TimeUnit.SECONDS.sleep(5);
        eventExecutors.shutdownGracefully();
    }

}
